package richbar.com.richbar.view.dialog;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.DatePicker;

import com.common.util.DateUtil;
import com.common.util.LogUtil;
import java.util.Calendar;

import richbar.com.richbar.R;

/**
 * Created by qyang on 2018-4-25.
 */

public class AndroidDateDialog {


    public void show(Context context, final Callback callback){
        Calendar c = Calendar.getInstance();
        final DatePickerDialog pickerDialog = new DatePickerDialog(context, R.style.DatePickerDialogTheme,new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
        pickerDialog.setCancelable(true);
        pickerDialog.setCanceledOnTouchOutside(true);
        pickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String date = new StringBuffer().append(pickerDialog.getDatePicker().getYear())
                                .append("-")
                                .append(DateUtil.getTwoPositionTime(pickerDialog.getDatePicker().getMonth() + 1))
                                .append("-")
                                .append(DateUtil.getTwoPositionTime(pickerDialog.getDatePicker().getDayOfMonth())).toString();
                        callback.onDate(date);
                    }
                });
        pickerDialog.show();
    }


    public void show(Context context,String date, final Callback callback){
        Calendar c = Calendar.getInstance();
        final DatePickerDialog pickerDialog = new DatePickerDialog(context, R.style.DatePickerDialogTheme,new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5,7));
        int day = Integer.parseInt(date.substring(8,10));
        LogUtil.e("PAPAPPAPAPA",year + "--" + month + "--" +day);
        if (month == 12){
            month = 11;
        }else {
            month = month -1;
        }
        pickerDialog.updateDate(year,month,day);
        pickerDialog.setCancelable(true);
        pickerDialog.setCanceledOnTouchOutside(true);
        pickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String date = new StringBuffer().append(pickerDialog.getDatePicker().getYear())
                                .append("-")
                                .append(DateUtil.getTwoPositionTime(pickerDialog.getDatePicker().getMonth() + 1))
                                .append("-")
                                .append(DateUtil.getTwoPositionTime(pickerDialog.getDatePicker().getDayOfMonth())).toString();
                        callback.onDate(date);
                    }
                });
        pickerDialog.show();
    }

    public interface Callback{
        void onDate(String date);
    }
}
